<!doctype html>
<html lang="en">
<head>
    <title>Code coverage report for src/assets/octopus/engine/security.js</title>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="..\..\..\..\prettify.css" />
    <link rel="stylesheet" href="..\..\..\..\base.css" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style type='text/css'>
        .coverage-summary .sorter {
            background-image: url(..\..\..\..\sort-arrow-sprite.png);
        }
    </style>
</head>
<body>
<div class='wrapper'>
  <div class='pad1'>
    <h1>
      <a href="..\..\..\..\index.html">All files</a> / <a href="index.html">src/assets/octopus/engine</a> security.js
    </h1>
    <div class='clearfix'>
      <div class='fl pad1y space-right2'>
        <span class="strong">0% </span>
        <span class="quiet">Statements</span>
        <span class='fraction'>0/57</span>
      </div>
      <div class='fl pad1y space-right2'>
        <span class="strong">0% </span>
        <span class="quiet">Branches</span>
        <span class='fraction'>0/32</span>
      </div>
      <div class='fl pad1y space-right2'>
        <span class="strong">0% </span>
        <span class="quiet">Functions</span>
        <span class='fraction'>0/16</span>
      </div>
      <div class='fl pad1y space-right2'>
        <span class="strong">0% </span>
        <span class="quiet">Lines</span>
        <span class='fraction'>0/57</span>
      </div>
    </div>
  </div>
  <div class='status-line low'></div>
<pre><table class="coverage">
<tr><td class="line-count quiet">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157</td><td class="line-coverage quiet"><span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-no">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span></td><td class="text"><pre class="prettyprint lang-js">function <span class="fstat-no" title="function not covered" >oSecurityEngine(engine) {</span>
<span class="cstat-no" title="statement not covered" >  this.passport = new oSecurityEngine_Passport(engine);</span>
<span class="cstat-no" title="statement not covered" >  this.token = new oSecurityEngine_Token(engine);</span>
<span class="cstat-no" title="statement not covered" >  this.auth = new oSecurityEngine_Auth(engine);</span>
}
&nbsp;
// 初始化原型链，包括护照、令牌、鉴权
function <span class="fstat-no" title="function not covered" >oSecurityEngine_Passport(engine) {</span>
<span class="cstat-no" title="statement not covered" >  this.engine = engine;</span>
}
&nbsp;
function <span class="fstat-no" title="function not covered" >oSecurityEngine_Token(engine) {</span>
<span class="cstat-no" title="statement not covered" >  this.engine = engine;</span>
}
&nbsp;
function <span class="fstat-no" title="function not covered" >oSecurityEngine_Auth(engine) {</span>
<span class="cstat-no" title="statement not covered" >  this.engine = engine;</span>
}
&nbsp;
<span class="cstat-no" title="statement not covered" >oSecurityEngine_Passport.prototype = {</span>
  get: <span class="fstat-no" title="function not covered" >function (</span>callback) {
    let o = <span class="cstat-no" title="statement not covered" >this;</span>
<span class="cstat-no" title="statement not covered" >    this.engine.request.getAjax("POST", "json", this.engine.config.url + "/ESB/Security/OAuth2/Passport", true, {</span>
      name: o.engine.passport.name, password: o.engine.passport.password, timespan: o.engine.passport.timespan
    }, <span class="fstat-no" title="function not covered" >function (</span>data) {
      // alert(JSON.stringify(data));
      // 判断护照是否正确获取到
<span class="cstat-no" title="statement not covered" >      if (!o.engine.tools.IsNullOrEmpty(data) &amp;&amp; data.status.code == 200) {</span>
<span class="cstat-no" title="statement not covered" >        o.engine.passport.id = data.content.id;</span>
<span class="cstat-no" title="statement not covered" >        o.engine.passport.realName = data.content.realName;</span>
<span class="cstat-no" title="statement not covered" >        o.engine.passport.language = data.content.language;</span>
<span class="cstat-no" title="statement not covered" >        o.engine.passport.appKey = data.content.appKey;</span>
        // 模板设置样式
<span class="cstat-no" title="statement not covered" >        o.engine.template.choiceTheme(data.content.theme);</span>
        // 设置权限
<span class="cstat-no" title="statement not covered" >        o.engine.passport.authorization = data.content.authorization;</span>
      }
      // 将请求数据回调给前段处理，里面包含所有状态的数据，通过内部的Status Code来识别。
<span class="cstat-no" title="statement not covered" >      if (callback) {</span>
<span class="cstat-no" title="statement not covered" >        callback(data);</span>
      }
    });
  },
}
<span class="cstat-no" title="statement not covered" >oSecurityEngine_Token.prototype = {</span>
  // 获取Token，自动处理localstorage中的护照数据，并自我分析、处理、合规的前提下返回Token。
  // Token获取成功，会自动触发callback事件，否则不会处理。
  get: <span class="fstat-no" title="function not covered" >function (</span>callback) {
    let o = <span class="cstat-no" title="statement not covered" >this;</span>
    // 获取Token，有效期为10分钟。
<span class="cstat-no" title="statement not covered" >    this.engine.request.getAjax("GET", "json", this.engine.config.url + "/ESB/Security/OAuth2/Token/" + this.engine.passport.appKey, true, null,</span>
<span class="fstat-no" title="function not covered" >      function (</span>data) {
        // 得到新的Token数据，如果有效，则更新当前的Passport数据。
<span class="cstat-no" title="statement not covered" >        if (!o.engine.tools.IsNullOrEmpty(data) &amp;&amp; data.status.code == 200) {</span>
<span class="cstat-no" title="statement not covered" >          o.engine.passport.token = data.content.passport.token;</span>
        }
&nbsp;
        // 处理完成后完整将返回结果回调，不管是正常的还是错误的，前端会处理。
<span class="cstat-no" title="statement not covered" >        if (callback) {</span>
<span class="cstat-no" title="statement not covered" >          callback(data);</span>
        }
      });
  },
  exists: <span class="fstat-no" title="function not covered" >function (</span>callback) {
    // 获取Token，有效期为10分钟。
<span class="cstat-no" title="statement not covered" >    this.engine.request.getAjax("GET", "json", this.engine.config.url + "/ESB/Security/OAuth2/Token/Exists/" + this.engine.passport.token, true, null,</span>
<span class="fstat-no" title="function not covered" >      function (</span>data) {
        // 处理完成后回调。
<span class="cstat-no" title="statement not covered" >        if (callback) {</span>
<span class="cstat-no" title="statement not covered" >          callback(data);</span>
        }
      });
  }
}
// 判断token是否合法
oSecurityEngine_Auth.prototype = {
  // 获取授权，判断Token是否存在，如果存在，则调用token.exist判断是否有效（Token应该和Passport一样，else返回错误包，而不是直接处理）
  // 如果不存在，则调用token.get获取新的token，如果创建token失败，说明appKey已经过期，提示【保存页面内容，并重新登录】。
  get: <span class="fstat-no" title="function not covered" >function (</span>url, callback) {
    let o = <span class="cstat-no" title="statement not covered" >this;</span>
    // 如果护照数据存在token，则发起token验证，看是否存在
<span class="cstat-no" title="statement not covered" >    if (!this.engine.tools.IsNullOrEmpty(this.engine.passport.token)) {</span>
      //判断是否有效，如果有效则返回对应授权
<span class="cstat-no" title="statement not covered" >      this.engine.request.security.token.exists(<span class="fstat-no" title="function not covered" >function (</span>data) {</span>
        // 返回200的时候data为当前token的完整数据包。
<span class="cstat-no" title="statement not covered" >        if (!o.engine.tools.IsNullOrEmpty(data) &amp;&amp; data.status.code == 200) {</span>
<span class="cstat-no" title="statement not covered" >          o.engine.tips.debug("Auth： 找到Token，返回：" + JSON.stringify(data));</span>
<span class="cstat-no" title="statement not covered" >          assemble();</span>
        }
        // 如果不对有可能是因为过期，重新请求一次。
        else {
<span class="cstat-no" title="statement not covered" >          o.engine.tips.debug("Auth： Token不存在，返回：" + JSON.stringify(data));</span>
          // token过期，重试token。
<span class="cstat-no" title="statement not covered" >          o.engine.request.security.token.get(<span class="fstat-no" title="function not covered" >function (</span>token_data) {</span>
<span class="cstat-no" title="statement not covered" >            if (!o.engine.tools.IsNullOrEmpty(token_data) &amp;&amp; token_data.status.code == 200) {</span>
<span class="cstat-no" title="statement not covered" >              o.engine.tips.debug("Auth： Token获取成功，返回：" + JSON.stringify(token_data));</span>
<span class="cstat-no" title="statement not covered" >              assemble();</span>
            } else {
<span class="cstat-no" title="statement not covered" >              o.engine.tips.debug("Auth： Token创建失败，准备重新申请Passport，返回：" + JSON.stringify(token_data));</span>
              // 护照过期，这里尝试重新申请，为了防止密码账户错误导致请求护照死循环，故只尝试1次护照的重新申请。
<span class="cstat-no" title="statement not covered" >              if (token_data.status.code == 300009) {</span>
                // 重新申请护照 + 令牌，只重试1次，防止死循环。
<span class="cstat-no" title="statement not covered" >                o.engine.request.security.passport.get(<span class="fstat-no" title="function not covered" >function (</span>passport_data) {</span>
<span class="cstat-no" title="statement not covered" >                  if (!o.engine.tools.IsNullOrEmpty(passport_data) &amp;&amp; passport_data.status.code == 200) {</span>
<span class="cstat-no" title="statement not covered" >                    o.engine.tips.debug("Auth： 护照创建成功，返回：" + JSON.stringify(passport_data));</span>
<span class="cstat-no" title="statement not covered" >                    o.engine.request.security.token.get(<span class="fstat-no" title="function not covered" >function (</span>token_create_data) {</span>
<span class="cstat-no" title="statement not covered" >                      o.engine.tips.debug("Auth： Token创建成功，返回：" + JSON.stringify(token_create_data));</span>
<span class="cstat-no" title="statement not covered" >                      assemble();</span>
                    });
                  } else {
<span class="cstat-no" title="statement not covered" >                    o.engine.tips.debug("Auth：护照创建失败，返回：" + JSON.stringify(passport_data));</span>
<span class="cstat-no" title="statement not covered" >                    o.engine.tips.error(passport_data);</span>
                    // 无力回天，护照都申请失败了，说明护照数据被篡改或用户修改了密码，必须重新登录了。
<span class="cstat-no" title="statement not covered" >                    o.engine.router.jumplogin();</span>
                  }
                });
              } else {
                // 进入此处说明护照根数据都没找到，肯定要重新登录了。。
<span class="cstat-no" title="statement not covered" >                o.engine.tips.error(data);</span>
                // 无力回天，护照都申请失败了，说明护照数据被篡改或用户修改了密码，必须重新登录了。
<span class="cstat-no" title="statement not covered" >                o.engine.router.jumplogin();</span>
              }
            }
          });
        }
      });
    } else {
      // this.engine.tips.debug("Auth： Token为空。");
      // this.engine.request.security.token.get(function (data) {
      //   if (!o.engine.tools.IsNullOrEmpty(data) &amp;&amp; data.status.code == 200) {
      //     assemble();
      //   } else {
      //     // 如果尝试获取还是失败，则直接返回错误状态。
      //     o.engine.tips.error(data);
      //     // 无力回天，Token获取需要依靠AppKey，失败说明护照申请失败，说明护照数据被篡改或用户修改了密码，必须重新登录了。
<span class="cstat-no" title="statement not covered" >      this.engine.router.jumplogin();</span>
      //   }
      // });
    }
&nbsp;
    // 装配url并回调事件
    function <span class="fstat-no" title="function not covered" >assemble() {</span>
      let auth_url = <span class="cstat-no" title="statement not covered" >o.engine.config.url + url;</span>
<span class="cstat-no" title="statement not covered" >      auth_url += auth_url.indexOf("?") &gt; 0 ? "&amp;" : "?";</span>
<span class="cstat-no" title="statement not covered" >      auth_url += "access_token=" + o.engine.passport.token;</span>
<span class="cstat-no" title="statement not covered" >      callback(auth_url);</span>
    }
  },
}
&nbsp;
&nbsp;
export {
  oSecurityEngine
}
&nbsp;
&nbsp;
&nbsp;</pre></td></tr>
</table></pre>
<div class='push'></div><!-- for sticky footer -->
</div><!-- /wrapper -->
<div class='footer quiet pad2 space-top1 center small'>
  Code coverage
  generated by <a href="https://istanbul.js.org/" target="_blank">istanbul</a> at Mon Jan 20 2020 17:54:15 GMT+0800 (GMT+08:00)
</div>
</div>
<script src="..\..\..\..\prettify.js"></script>
<script>
window.onload = function () {
        if (typeof prettyPrint === 'function') {
            prettyPrint();
        }
};
</script>
<script src="..\..\..\..\sorter.js"></script>
</body>
</html>
